package cn.openea.perpartner.componet.web.service.importandexport;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.componet.constant.CommonConstants;
import cn.openea.perpartner.componet.web.entity.SmsBlacklist;
import cn.openea.perpartner.componet.web.entity.dto.ImportErrMsgDTO;
import cn.openea.perpartner.componet.web.entity.dto.ImportSmsBlacklistDTO;
import cn.openea.perpartner.componet.web.entity.vo.ExportDataVo;
import cn.openea.perpartner.componet.web.service.ISmsBlacklistService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 导入短信黑名单处理
 * @createTime 2023年06月13日 19:06:00
 */
@Slf4j
@Service(value = CommonConstants.IMPORTANDEXPORT.IMPORT_SMS_BLACKLIST)
public class ImportSmsBlacklistService extends ImportAndExportAbstractService {
    @Resource
    private ISmsBlacklistService smsBlacklistService;

    @Override
    public void exportExcel(ExportDataVo exportDataVo, HttpServletResponse response) {
        // 导出模板
        List<ImportSmsBlacklistDTO> list = new ArrayList<>();
        ExcelUtil<ImportSmsBlacklistDTO> util = new ExcelUtil<>(ImportSmsBlacklistDTO.class);
        util.exportExcel(response, list, "导入短信黑名单模版.xlsx");
    }

    @Override
    protected ImportErrMsgDTO handleSingleExcel(@NonNull Object obj) {
        if (!(obj instanceof ImportSmsBlacklistDTO)){
            throw new ServiceException("数据异常");
        }
        ImportSmsBlacklistDTO smsBlacklistDTO = (ImportSmsBlacklistDTO) obj;
        try {
            smsBlacklistService.add(SmsBlacklist.builder()
                    .phone(smsBlacklistDTO.getPhone())
                    .reasons(smsBlacklistDTO.getReasons())
                    .build());
        }catch (Exception e){
            return ImportErrMsgDTO.builder()
                    .id(smsBlacklistDTO.getPhone())
                    .errMsg(e.getMessage())
                    .build();
        }
        return null;
    }
}
